package com.watson.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.watson.BaseActivity;
import com.watson.R;
import com.watson.mq.RabbitMQUtil;
import com.watson.mq.ReceiveMessageTask;
import com.watson.mq.SendMessageTask;

public class CeshiActivity extends BaseActivity {
    private TextView showMessageTv;
    private EditText contentEt;
    private StringBuilder sb = new StringBuilder();

    //自己配置发送接受队列名称和绑定key
    private final String sendQueueOne = "testOne";
    private final String sendRoutingKey = "two.rabbit.ok";
    private final String receiveQueueOne = "testOne";
    private final String receiveQueueTwo = "testTwo";
    private final String receiveRoutingKey = "two.#";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ceshi);
        statusbar4();
        contentEt = findViewById(R.id.et_content);
        showMessageTv = findViewById(R.id.tv_show_message);
        showMessageTv.setMovementMethod(ScrollingMovementMethod.getInstance());

        //TODO 内容、状态、跳转
        //这是调用的
// 发送
//        new SendMessageTask().execute("哈哈哈！真的不容易啊？");
// 接收
//        new ReceiveMessageTask().execute();

//        RabbitMQUtil.initService("mm.vocen.com.cn", 5655, "wms", "wms@@123");
////        RabbitMQUtil.initService("192.168.2.121", 5672, "ai", "ai");
//        RabbitMQUtil.initExchange("wms", "topic");
//
//        findViewById(R.id.btn_send_queue).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                final String message = contentEt.getText().toString().trim();
//
//                if (TextUtils.isEmpty(message)) {
//                    Toast.makeText(mContext, "消息不能为空！！！", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                RabbitMQUtil.getInstance().sendQueueMessage(message, sendQueueOne, isSuccess -> runOnUiThread(() -> {
//                    if (isSuccess) {
//                        sb.append("发送队列消息：").append(message).append("\n");
//                        showMessageTv.setText(sb);
//                        contentEt.setText("");
//                    } else {
//                        Toast.makeText(mContext, "发送消息失败，请检查网络后稍后再试！！！", Toast.LENGTH_SHORT).show();
//                    }
//                }));
//            }
//        });
    }

    @Override
    protected void onDestroy() {
        RabbitMQUtil.getInstance().close();
        super.onDestroy();
    }

    public void sendRouting(View view) {
        final String message = contentEt.getText().toString().trim();

        if (TextUtils.isEmpty(message)) {
            Toast.makeText(mContext, "消息不能为空！！！", Toast.LENGTH_SHORT).show();
            return;
        }

        RabbitMQUtil.getInstance().sendRoutingKeyMessage(message, sendRoutingKey, new RabbitMQUtil.SendMessageListener() {
            @Override
            public void sendMessage(final boolean isSuccess) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (isSuccess) {
                            sb.append("发送routing消息：").append(message).append("\n");
                            contentEt.setText("");
                            showMessageTv.setText(sb);

                        } else {
                            Toast.makeText(mContext, "发送消息失败，请检查网络后稍后再试！！！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

    public void listenQueue(View view) {
        RabbitMQUtil.getInstance().receiveQueueMessage(receiveQueueOne, new RabbitMQUtil.ReceiveMessageListener() {
            @Override
            public void receiveMessage(String message) {
                sb.append("收到了queue消息：").append(message).append("\n");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showMessageTv.setText(sb);
                    }
                });
            }
        });
    }

    public void listenRouting(View view) {
        RabbitMQUtil.getInstance().receiveRoutingKeyMessage(receiveRoutingKey, new RabbitMQUtil.ReceiveMessageListener() {
            @Override
            public void receiveMessage(String message) {
                sb.append("收到了routing消息：").append(message).append("\n");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showMessageTv.setText(sb);
                    }
                });
            }
        });
    }
}
